<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    /**
     * Initialize the autoloader
     *
     * @return Zend_Application_Module_Autoloader
     */
    protected function _initAutoload()
    {
        // Ensure front controller instance is present
        $this->bootstrap('frontController');
        // Get frontController resource
        $this->_front = $this->getResource('frontController');

        // Add autoloader empty namespace
        $autoLoader =  new Zend_Loader_Autoloader_Resource(array(
            'basePath'      => APPLICATION_PATH,
            'namespace'     => '',
            'resourceTypes' => array(
                'form' => array(
                    'path'      => 'admin/forms/',
                    'namespace' => 'Admin_Form_',
                ),
                'model' => array(
                    'path'      => 'admin/models/',
                    'namespace' => 'Model_'
                ),
                'nette' => array(
                    'path'      => '../library/Nette',
                    'namespace' => 'Nette_'
                )
            ),
        ));
        // Return it, so that it can be stored by the bootstrap
        return $autoLoader;
    }

    protected function _initDebug()
    {
        Nette_Diagnostics_Debugger::_init();
        Nette_Diagnostics_Debugger::enable(
            'production' == APPLICATION_ENV,
            BASE_PATH . '/log',
            'roman.gnatjuk@gmail.com'
        );
        Nette_Diagnostics_Debugger::$strictMode = 1;
        Nette_Diagnostics_Debugger::$maxDepth = 3;
        Nette_Diagnostics_Debugger::$maxLen = 150;
        //Nette_Diagnostics_Debugger::$consoleMode = 1;
    }

    /**
     * Initialize the local php configuration
     *
     * @return void
     */
    protected function _initPhpConfig()
    {
    }

    /**
     * Initialize the site configuration
     *
     * @return Zend_Config_Xml
     */
    protected function _initConfig()
    {
        // Retrieve configuration from file
        $config = new Zend_Config_Xml(APPLICATION_PATH . '/admin/data/config.xml', APPLICATION_ENV);

        // Add config to the registry so it is available sitewide
        Zend_Registry::set('config', $config);
        // Return it, so that it can be stored by the bootstrap
        return $config;
    }

    protected function _initCache()
    {
         // Cache options
        $frontendOptions = array(
            'lifetime' => 1200,                      // Cache lifetime of 20 minutes
            'automatic_serialization' => true,
        );
        $backendOptions = array(
            'cache_dir' => BASE_PATH . '/cache/',   // Directory where to put the cache files
        );
        // enable Plugin Loader Cache - see ZF reference chapter 30.4.4.
        $classFileIncCache = $backendOptions['cache_dir'] . 'pluginLoaderCache.php';
        if (file_exists($classFileIncCache)) {
            include_once $classFileIncCache;
        }
        Zend_Loader_PluginLoader::setIncludeFileCache($classFileIncCache);
        // Get a Zend_Cache_Core object
        $cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);

        // Set cache for Zend_Db_Table - see ZF reference chapter 15.5.12.
        Zend_Db_Table_Abstract::setDefaultMetadataCache($cache);

        Zend_Registry::set('cache', $cache);
        // Return it, so that it can be stored by the bootstrap

        return $cache;
    }

    /**
     * Initialize data base
     *
     * @return Zend_Db_Adapter_Pdo_Abstract
     */
    protected function _initDb()
    {
        /** @var Zend_Application_Resource_Db $resource  */
        $resource = $this->getPluginResource('db');
        /** @var Zend_Db_Adapter_Pdo_Mysql $db */
        $db = $resource->getDbAdapter();
        $db->setFetchMode(Zend_Db::FETCH_OBJ);
        Zend_Db_Table::setDefaultAdapter($db);
        Zend_Registry::set('database', $db);
        return $db;
    }

    /**
     * Initialize the site settings
     *
     * @return stdObject
     */
    protected function _initSiteSettings()
    {
        $siteSettings = new Model_SiteSettings();
        Zend_Registry::set('siteSettings', $siteSettings);
        // Return it, so that it can be stored by the bootstrap
        return $siteSettings;
    }

    /**
     * Initialize the site's locale
     *
     * @return Zend_Locale
     */
    protected function _initLocale()
    {
        $this->bootstrap('config');
        // Get config resource
        $config = $this->getResource('config');

        $this->bootstrap('cache');
        // Get cache object
        $cache = $this->getResource('cache');
        Zend_Locale::setCache($cache);

        $this->bootstrap('siteSettings');
        // Get siteSettings object
        $siteSettings = $this->getResource('siteSettings');

        // Set default locale
        $locale = new Zend_Locale($config->language->defaultLocale);

        // Set default timezone
        $timezone = $siteSettings->get('default_timezone');
        date_default_timezone_set($timezone);
        // Return it, so that it can be stored by the bootstrap
        return $locale;
    }

    /**
     * Initialize the view
     *
     * @return Zend_View
     */
    protected function _initView()
    {
        // Initialize view
        $view = new Zend_View();

        $this->bootstrap('siteSettings');
        // Get settings resource
        $settings = $this->getResource('siteSettings');
        // Set doctype and charset
        $view->doctype($settings->get('doc_type'));
        $view->placeholder('charset')->set($settings->get('default_charset'));

        $view->addFilterPath('Core/View/Filter', 'Core_View_Filter');
        $view->setFilter('Translate');

        // Add the view to the ViewRenderer
        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
            'ViewRenderer'
        );
        $viewRenderer->setView($view);

        // base helpers
        $view->addHelperPath('Core/View/Helper',     'Core_View_Helper');
        $view->addHelperPath('Core/Content/Control', 'Core_Content_Control');

        // Load core helpers
        $helperDirs = Core_Filesystem_Dir::getDirectories(BASE_PATH . '/library/Core/View/Helper');
        if (is_array($helperDirs)) {
            foreach ($helperDirs as $dir) {
                $view->addHelperPath(BASE_PATH . '/library/Core/View/Helper/' . $dir, 'Core_View_Helper_' . ucfirst($dir));
            }
        }
        $view->baseUrl = $this->_front->getBaseUrl();
        // Return it, so that it can be stored by the bootstrap
        return $view;
    }

    /**
     * Initialize the view filters
     *
     * @return void
     */

    protected function _initViewFilter()
    {
        // get view resource
        $view = $this->getResource('view');
        // base filters
        $view->addFilterPath('Core/View/Filter', 'Core_View_Filter');
        foreach (Core_Filesystem_File::getFilesByType(BASE_PATH . '/library/Core/View/Filter', 'php', false, false) as $filter) {
            $filter = ucfirst($filter);
            if ('Exception' != $filter) {
                $view->addFilter($filter);
            }
        }
    }

    /**
     * Initialize the controllers
     *
     * @return void
     */
    protected function _initControllers()
    {
        // Setup core cms modules
        $this->_front->addControllerDirectory(APPLICATION_PATH . '/admin/controllers',  'admin');
        $this->_front->addControllerDirectory(APPLICATION_PATH . '/public/controllers', 'public');

        // Setup extension modules
        $this->_front->setParam('pathToModules', APPLICATION_PATH . '/modules');
        $cmsModules = null;
        if ($modules = Core_Module::getModules()) {
            foreach ($modules as $module) {
                $cmsModules['mod_' . $module] = $module;
                $this->_front->addControllerDirectory(APPLICATION_PATH . '/modules/' . $module . '/controllers', 'mod_' . $module);
            }
        }
        if (is_array($cmsModules)) {
            $this->_front->setParam('cmsModules', $cmsModules);
        }
    }
}